package com.androidexplosion.takeitback.provider;

//import java.util.Date;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;

import com.androidexplosion.takeitback.entity.Remind;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class RemindDBAdapter {
	private static final String DATABASE_NAME = "TakeItBack";
	protected static final String DATABASE_TABLE_REMINDER = "reminds";
	private static final int DATABASE_VERSION = 1;
	protected SQLiteDatabase db;
	private final Context context;

	public final static String REMIND_ID = "ReminderID";
	public final static String REMIND_OBJECT = "Object";
	public final static String REMIND_PERSON = "Person";
	public final static String REMIND_ALARM_DATE = "AlarmDate";
	public final static String REMIND_ALARM_TIME = "AlarmTime";

	private toDoDBOpenHelper dbHelper;

	public RemindDBAdapter(Context _context) {
		this.context = _context;
		dbHelper = new toDoDBOpenHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
	}

	// Remove a task based on its index
	public boolean deleteRemind(long _rowIndex) {
		return db.delete(DATABASE_TABLE_REMINDER, REMIND_ID + "=" + _rowIndex,
				null) > 0;
	}

	// Insert a new task
	public long addRemind(Remind _remind) {
		// Create a new row of values to insert.
		ContentValues newValue = new ContentValues();
		// Assign values for each row.
		newValue.put(REMIND_OBJECT, _remind.getRemindObject());
		newValue.put(REMIND_ALARM_DATE, _remind.getDate().getTime());
		newValue.put(REMIND_PERSON, _remind.getPerson());
		newValue.put(REMIND_ALARM_TIME, _remind.getTime().getTime());
		// Insert the row.
		return db.insert(DATABASE_TABLE_REMINDER, null, newValue);
	}

	public long addRemind(String _object, String _person, long _date, long _time) {
		// Create a new row of values to insert.
		ContentValues newValue = new ContentValues();
		// Assign values for each row.
		newValue.put(REMIND_OBJECT, _object);
		newValue.put(REMIND_PERSON, _person);
		newValue.put(REMIND_ALARM_DATE, _date);
		newValue.put(REMIND_ALARM_TIME, _time);
		// Insert the row.
		return db.insert(DATABASE_TABLE_REMINDER, null, newValue);
	}

	// Update a task
	public boolean updateRemind(Remind _remind) {
		ContentValues newValue = new ContentValues();
		newValue.put(REMIND_OBJECT, _remind.getRemindObject());
		newValue.put(REMIND_ALARM_DATE, _remind.getDate().getTime());
		newValue.put(REMIND_PERSON, _remind.getPerson());
		newValue.put(REMIND_ALARM_TIME, _remind.getTime().getTime());
		return db.update(DATABASE_TABLE_REMINDER, newValue, REMIND_ID + "="
				+ _remind.getRemindID(), null) > 0;
	}

	public Cursor getAllRemindsCursor() {
		return db.query(DATABASE_TABLE_REMINDER, new String[] { REMIND_ID,
				REMIND_OBJECT, REMIND_ALARM_DATE, REMIND_PERSON }, null, null,
				null, null, null);
	}

	public ArrayList<Remind> getAllReminds() {
		ArrayList<Remind> reminds = new ArrayList<Remind>();
		Cursor remindListCursor = db.query(DATABASE_TABLE_REMINDER,
				new String[] { REMIND_ID, REMIND_OBJECT, REMIND_PERSON,
						REMIND_ALARM_DATE, REMIND_ALARM_TIME }, null, null,
				null, null, null);

		if (remindListCursor.moveToFirst())
			do {
				int id = remindListCursor.getInt(remindListCursor
						.getColumnIndex(RemindDBAdapter.REMIND_ID));
				String object = remindListCursor.getString(remindListCursor
						.getColumnIndex(RemindDBAdapter.REMIND_OBJECT));
				String person = remindListCursor.getString(remindListCursor
						.getColumnIndex(RemindDBAdapter.REMIND_PERSON));
				long date = remindListCursor.getLong(remindListCursor
						.getColumnIndex(RemindDBAdapter.REMIND_ALARM_DATE));
				long time = remindListCursor.getLong(remindListCursor
						.getColumnIndex(RemindDBAdapter.REMIND_ALARM_TIME));

				Remind newRemind = new Remind(id, object, person,
						new Date(date), new Time(time));
				reminds.add(0, newRemind);
			} while (remindListCursor.moveToNext());

		return reminds;
	}

	public Cursor setCursorToRemind(long _rowIndex) throws SQLException {
		Cursor result = db.query(true, DATABASE_TABLE_REMINDER, new String[] {
				REMIND_ID, REMIND_OBJECT, REMIND_PERSON, REMIND_ALARM_DATE,
				REMIND_ALARM_TIME }, REMIND_ID + "=" + _rowIndex, null, null,
				null, null, null);
		if ((result.getCount() == 0) || !result.moveToFirst()) {
			throw new SQLException("No to do items found for row: " + _rowIndex);
		}
		return result;
	}

	public void close() {
		db.close();
	}

	public void open() throws SQLiteException {
		try {
			db = dbHelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = dbHelper.getReadableDatabase();
		}
	}

	private static class toDoDBOpenHelper extends SQLiteOpenHelper {
		public toDoDBOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		// SQL Statement to create a new database.
		private static final String DATABASE_CREATE_REMINDER = "create table "
				+ DATABASE_TABLE_REMINDER + " (" + REMIND_ID
				+ " integer primary key autoincrement, " + REMIND_OBJECT
				+ " text not null, " + REMIND_PERSON + " text,"
				+ REMIND_ALARM_DATE + " long, " + REMIND_ALARM_TIME + " long);";

		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(DATABASE_CREATE_REMINDER);
		}

		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
				int _newVersion) {
			Log.w("RemindDBAdapter", "Upgrading from version " + _oldVersion
					+ " to " + _newVersion
					+ ", which will destroy all old data");
			// Drop the old table.
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_REMINDER);
			// Create a new one.
			onCreate(_db);
		}
	}
}